
"""
分析报告模块
"""
from core.data_analyze.state import AnalyzeState
from core.data_analyze.prompts import data_analyze_prompts
from langgraph.types import Command
from langchain_core.messages import AIMessage
from langgraph.config import get_stream_writer
from core.llm import llm
from loguru import logger


def report_node(state: AnalyzeState):

    writer = get_stream_writer()
    writer("## **生成数据分析报告**")
    logger.info("## **生成数据分析报告**")
    template_prompt = data_analyze_prompts.get('report')
    prompt = template_prompt.format(
        inputs={
            "task_description": state.get('revised_task'),
            "analyze_content": state.get("analyze_content")
        },
        remove_template_variables=True
    )
    report_content = llm.invoke(prompt).content
    writer(report_content)
    logger.info(report_content)

    return Command(
        update={
            'messages': [AIMessage(content=report_content, name='report')],
            'report_content': report_content,
        },
        goto="__end__"
    )